package com.homegoods.discover.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientTitleBanner;
import com.bigkoo.convenientbanner.PageIndicatorAlign;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.homegoods.discover.R;
import com.homegoods.discover.model.BannerModel;
import com.homegoods.resource.ILifecycle;
import com.kedll.library.util.BindingUtil;
import com.kedll.library.util.MyUtils;

import java.util.List;


/**
 * TODO: document your custom view class.
 *
 * @author QiQi
 */
public class HeadView extends FrameLayout implements ILifecycle {

    ConvenientTitleBanner<BannerModel> convenientBanner;
    OnItemClickListener onBannerClickListener;

    int adWidth;
    int adHeight;

    public HeadView(Context context) {
        this(context, null);
    }

    public HeadView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public HeadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.discover_best_collect_head_layout, this, true);

        adWidth = MyUtils.getDisplayWidth(context);
        adHeight = MyUtils.dp2px(100);
        convenientBanner = findViewById(R.id.convenientBanner);
    }


    public void setOnBannerClickListener(OnItemClickListener l) {
        convenientBanner.setOnItemClickListener(l);
    }


    /**
     * 设置广告
     */
    public void setPage(List<BannerModel> images) {
        convenientBanner.setPages(
                (CBViewHolderCreator<NetWorkImageHolderView>) NetWorkImageHolderView::new, images)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                .setPageIndicator(new int[]{R.mipmap.indicator_normal, R.mipmap.indicator_select})
                //设置指示器的方向
                .setPageIndicatorAlign(PageIndicatorAlign.ALIGN_PARENT_RIGHT);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        onStop();
    }


    public synchronized void setRunning(boolean isRunning) {
        if (!isRunning) {
            onStop();
        } else {
            onStart();
        }
    }

    @Override
    public void onStart() {
        convenientBanner.startTurning(3000);
    }

    @Override
    public void onStop() {
        convenientBanner.stopTurning();
    }

    @Override
    public void onDestroy() {
    }


    private class NetWorkImageHolderView implements Holder<BannerModel> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            return imageView;
        }

        @Override
        public void updateUI(Context context, int position, BannerModel data) {
            loadImageByVolley(data);
            convenientBanner.setTitle(data.getTitle());
        }


        /**
         * <p/>
         * 注意方法参数:
         * getImageListener(ImageView view, int defaultImageResId, int errorImageResId)
         * 第一个参数:显示图片的ImageView
         * 第二个参数:默认显示的图片资源
         * 第三个参数:加载错误时显示的图片资源
         */
        private void loadImageByVolley(BannerModel imageUrl) {
            BindingUtil.loadImageNoTransform(imageView, imageUrl.toString(), null, null);

        }
    }
}